<?php
class Current_User {

	private static $user;

	private function __construct() {}

	public static function user() {

		if(!isset(self::$user)) {

			if (!$user_id = $_SESSION['user_id']) {
				return FALSE;
			}

			if (!$u = Doctrine::getTable('Users')->find($user_id)) {
				return FALSE;
			}

			self::$user = $u;
		}

		return self::$user;
	}

	public static function login($email_id, $password) {

		// get User object by username
		if ($u = Doctrine::getTable('Users')->findOneByEmail_id($email_id)) {

			// this mutates (encrypts) the input password
			$u_input = new Users();
			$u_input->password = $password;

			// password match (comparing encrypted passwords)
			if ($u->password == $u_input->password) {
				unset($u_input);

				$newdata = array(
                   					'user_id'  => $u->user_id,
									'email_id' => $u->email_id,
									'first_name' => $u->first_name,
                   					'logged_in' => 'TRUE'
                   					);

                $_SESSION['userdata'] = $newdata;
                
                self::$user = $u;
				return $newdata;
			}

			unset($u_input);
		}
		//echo "<Br>Login failed";
		// login failed
		return FALSE;

	}

	public function __clone() {
		trigger_error('Clone is not allowed.', E_USER_ERROR);
	}

	

	

}
